package org.jeecg.modules.shextdj.Job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.shextdj.entity.AgShextdj;
import org.jeecg.modules.shextdj.entity.DetaShextData;
import org.jeecg.modules.shextdj.entity.DetaShextRecord;
import org.jeecg.modules.shextdj.service.IAgShextdjService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class syncShext implements Job {
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        syncData();
    }
    @Autowired
    private IAgShextdjService agShextdjService;

    @Autowired
    RestTemplate restTemplate;
    boolean yetFinishSync = false;

    // 同步德塔摄像头数据
    public Result <?> syncData() throws Exception {
        System.out.println("同步德塔摄像头数据");
        // 如果上次没同步完，直接返回
        if(yetFinishSync) {
            System.out.println("yet over");
            return null;
        }

        Map <String, Object> map = new HashMap <>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("page", 1);
        map2.put("size", 5000);
        map.put("pagination", map2);

        map2 = new HashMap<>();
        map2.put("direction", "DESC");
        List list2 = new ArrayList();
        list2.add("time");
        map2.put("fields", list2);
        map.put("sort", map2);
        map.put("t", DateUtils.getTimestamp());

        System.out.println(JSONObject.toJSON(map).toString());
        DetaShextData detaShextData = restTemplate.postForObject("http://26.29.23.93:8080/dw/sxtsy3344", map, DetaShextData.class);
        if(detaShextData != null) {
//            agShextdjService.remove(null);

            System.out.println(detaShextData.getRecords().size());
            for (String str: detaShextData.getRecords()) {
                DetaShextRecord detaShextRecord = JSON.parseObject(str, new TypeReference <DetaShextRecord>(){});
                if(detaShextData != null) {
                    AgShextdj agShextdj = new AgShextdj();

                    agShextdj.setCode(detaShextRecord.get_id());
                    agShextdj.setChangs(detaShextRecord.getManufacturer());
                    agShextdj.setShebmc(detaShextRecord.getPosition() + "_" + detaShextRecord.getManufacturer());
                    agShextdj.setAnzwz(detaShextRecord.getPosition());
                    agShextdj.setGpsJingd(detaShextRecord.getLongitude());
                    agShextdj.setGpsWeid(detaShextRecord.getLatitude());
                    agShextdj.setAnzgd(detaShextRecord.getHeight());
                    agShextdj.setWs("ws://26.29.23.93:8086/" + URLEncoder.encode(detaShextRecord.getRtsp()));
                    agShextdj.setRtsp(detaShextRecord.getRtsp());
                    agShextdj.setBeiz(detaShextRecord.getRemarks());

                    AgShextdj agShextdj_exists = agShextdjService.getOne(new QueryWrapper <AgShextdj>().eq("bianh", agShextdj.getCode()), false);
                    if(agShextdj_exists != null) {
                        agShextdj.setId(agShextdj_exists.getId());
                        agShextdjService.updateById(agShextdj);
                    }
                    else
                        agShextdjService.save(agShextdj);
                }
            }

            return Result.ok();
        }

        return Result.error("同步数据失败");
    }

}
